/**
 * @class   wosh::gui::DialogCreateGui
 * @brief   
 *          
 *          
 *
 ****************************************************************************
 * @version $Id: DialogCreateGui.h 2184 2010-03-20 13:19:23Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_DialogCreateGui_H__
 #define __WOSH_Gui_DialogCreateGui_H__

 #include <core/Request.h>
 #include <core/Response.h>
 #include <core/NEW_List.h>
 #include <core/InterfacesProvider.h>
 #include <core/MethodsDef.h>
 #include <core/NEW_List.h>
 #include <core/URI.h>

 #include <framework/gui/IWidget.h>

// #include <QtGui>
 #include <QtGui/QDialog>

 #include "ui_DialogCreateGui.h"


namespace wosh {
 namespace gui {


class DialogCreateGui : public QDialog, public IWidget  {

	Q_OBJECT

	public:

		DialogCreateGui( QWidget *parent = NULL ) : QDialog(parent), IWidget() {
			this->m_ui = new Ui::DialogCreateGui();
			this->m_ui->setupUi(this);

			this->setWindowTitle( tr("Create GUI for..") );
			this->getWidgetURI().setName("DialogCreateGui");
			this->getTargetURI().setAny();

			QObject::connect(this->m_ui->btnAutoDetect, SIGNAL(clicked()), this, SLOT(btnAutoDetect_clicked()));
			QObject::connect(this->m_ui->btnRemove, SIGNAL(clicked()), this, SLOT(btnRemove_clicked()));
			QObject::connect(this->m_ui->cmbType, SIGNAL(editTextChanged(const QString&)), this, SLOT(txtType_editTextChanged(const QString&)));

			QObject::connect(this->m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
			QObject::connect(this->m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

			QObject::connect(this->m_ui->wUriSelector, SIGNAL(changedURI(const URI&)), this, SLOT(changedURI(const URI&)));
		 }

		virtual ~DialogCreateGui() {
			delete this->m_ui;
		 }


	public:
		virtual std::string& getName()				{ return this->itemName; }
		virtual std::string& getType()				{ return this->itemType; }
		virtual URI& getURI()						{ return this->itemURI; }
		virtual QStringList& getInterfaces()		{ return this->itemInterfaces; }


		virtual void setTypes( const std::vector<std::string>& types ) {
			this->m_ui->cmbType->clear();
			this->m_ui->cmbType->setEditText("");

			std::vector<std::string>::const_iterator it;
			for(it=types.begin(); it!=types.end(); it++) {
				this->m_ui->cmbType->addItem( (*it).c_str() );
			 }
		 }

	public:
		virtual void busMessage( const Message& message, const Bus* ) {
			if ( message.isEmpty() ) return;
			if ( !URI::pregMatch( this->myURI, message.getDestination() ) )
				return;
			if ( message.getContent()->isResponse() ) {
				const wosh::Response* response = message.getContent()->asResponse();
				if ( !response->hasData() )
					return;
				if ( response->getMethod() == _METHOD_ListInterfaces ) {
					if ( !response->getData()->isClass(_NEW_List_TYPE) )
						return;
					const wosh::NEW_List* intList = dynamic_cast<const wosh::NEW_List*>(response->getData());
					this->m_ui->wDataListInterfaces->clear();
					this->m_ui->wDataListInterfaces->loadList(intList);
					buildInterfacesList();
				 }
				else if ( response->getMethod() == _METHOD_GetProperty ) {
					if ( !response->getData()->isClass(_Property_TYPE) )
						return;
					const wosh::Property* propName = dynamic_cast<const wosh::Property*>(response->getData());
					if ( propName->getKey() == _KEY_name ) {
						this->itemName = propName->getValue().toString();
					 }
					else if ( propName->getKey() == _KEY_type ) {
						this->itemType = propName->getValue().toString();
						this->m_ui->cmbType->setEditText( this->itemType.c_str() );
					 }
				 }
			 }
		 }

		virtual QWidget* asQWidget()				{ return this; }


	public slots:
		virtual void refreshAll() {
			this->m_ui->wUriSelector->refreshAll();
		 }


	protected slots:

		void changedURI( const URI& uri ) {
			this->itemURI = uri;
		 }

		void btnAutoDetect_clicked() {
			this->setTargetURI(this->itemURI);
			postMessageForTarget( new Message(new Request(_METHOD_GetProperty, new Variant(_KEY_name))) );
			postMessageForTarget( new Message(new Request(_METHOD_GetProperty, new Variant(_KEY_type))) );
			postMessageForTarget( new Message(new Request(_METHOD_ListInterfaces)) );
		 }

		void btnRemove_clicked() {
			QListWidgetItem* item = this->m_ui->wDataListInterfaces->currentItem();
			if (item == NULL )
				return;
			this->m_ui->wDataListInterfaces->clearSelection();
			this->m_ui->wDataListInterfaces->removeItemWidget(item);
			buildInterfacesList();
		 }

		void txtType_editTextChanged( const QString& text ) {
			this->itemType.assign( text.toLatin1().data() );
		 }

	protected:
		void changeEvent(QEvent *e)	{
			QWidget::changeEvent(e);
			switch (e->type()) {
			case QEvent::LanguageChange:
				m_ui->retranslateUi(this);
				break;
			default:
				break;
			}
		 }

		void buildInterfacesList() {
			this->itemInterfaces.clear();
			QListWidgetItem* item = NULL;
			for(int i=0; i<this->m_ui->wDataListInterfaces->count(); i++) {
				item = this->m_ui->wDataListInterfaces->item(i);
				if ( item == NULL ) continue;
				this->itemInterfaces.append( item->text() );
			 }
		 }

	protected:
		std::string itemName;
		std::string itemType;
		URI itemURI;
		QStringList itemInterfaces;

	private:
		Ui::DialogCreateGui *m_ui;


}; // class def

 }; // namespace gui
}; // namespace wosh


#endif // __WOSH_Gui_DialogCreateGui_H__
